<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增维修店服务区域')" />
    <link href="../static/css/bootstrap-select.min.css" th:href="@{/css/bootstrap-select.min.css}" rel="stylesheet"/>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-repairArea-add">
            <input id="type" name="type" class="form-control" type="hidden" value="2">
            <input id="repairProvinceName" name="repairProvinceName" class="form-control" type="hidden">
            <input id="repairCityName" name="repairCityName" class="form-control" type="hidden">
            <input id="repairDistrictName" name="repairDistrictName" class="form-control" type="hidden">
            <input id="repairId" name="repairId" th:value="*{repairId}" class="form-control" type="hidden">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">所在区域：</label>
                <div class="col-sm-8">
                    <select class="selectpicker form-control" onchange="initCity()" id="repairProvince" name="repairProvince" required>
                        <option value="">&#45;&#45;省份&#45;&#45;</option>
                    </select>
                    <select class="selectpicker form-control" onchange="initRegion()" id="repairCity" name="repairCity" required>
                        <option value="">&#45;&#45;城市&#45;&#45;</option>
                    </select>
                    <select class="selectpicker form-control" id="repairDistrict" name="repairDistrict" required>
                        <option value="">&#45;&#45;县区&#45;&#45;</option>
                    </select>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script src="../static/js/bootstrap-select.min.js" th:src="@{/js/bootstrap-select.min.js}"></script>
    <script th:inline="javascript">
        var prefix = ctx + "zsyh/repairArea"
        $("#form-repairArea-add").validate({
            focusCleanup: true
        });


        function submitHandler() {
            if ($.validate.form()) {
                $("#repairProvinceName").val($('#repairProvince option:selected').text());
                $("#repairCityName").val($('#repairCity option:selected').text());
                $("#repairDistrictName").val($('#repairDistrict option:selected').text());
                $.operate.save(prefix + "/addRakerArea", $('#form-repairArea-add').serialize());
            }
        }

        $(document).ready(function(){
            // 地区下拉框初始化
            $("#repairProvince").selectpicker({width:130, liveSearch: true, liveSearchPlaceholder: "搜索" });
            $("#repairCity").selectpicker({width:120, liveSearch: true, liveSearchPlaceholder: "搜索" });
            $("#repairDistrict").selectpicker({width:120, liveSearch: true, liveSearchPlaceholder: "搜索" });

            // 动态加载省份选项数据
            initSelectOptions("repairProvince","0");
        });// onReady

        /**
         * 动态生成select选项
         * @param selectId
         * @returns
         */
        function initSelectOptions(selectId,parentId) {
            var selectObj = $("#" + selectId);
            $.ajax({
                url : ctx + "zsyh/factory/"+parentId,
                async : false,
                type : "GET",
                success : function(result) {
                    debugger;
                    var configs = result.rows;
                    selectObj.find("option:not(:first)").remove();
                    for (var i in configs) {
                        var addressConfig = configs[i];
                        var optionValue = addressConfig.id;
                        var optionText = addressConfig.areaName;
                        selectObj.append(new Option(optionText, optionValue));
                    }

                    // 刷新select
                    selectObj.selectpicker('refresh');
                },
                error : function(result) {
                    toastr.error('获取['+ parentId + ']信息失败，原因：' + result.errorMessage);
                }
            });// ajax
        }

        /**
         * 根据选择的省份动态初始化城市options
         * @returns
         */
        function initCity() {
            // 当省份变动时，初始化城市和清空县区和街道
            var provinceSel = $("#repairProvince").val();
            initSelectOptions("repairCity", provinceSel);
            $("#repairDistrict").find("option:not(:first)").remove();
            $("#repairDistrict").selectpicker('refresh');
        }

        /**
         * 根据选择的城市动态初始化区域options
         * @returns
         */
        function initRegion() {
            // 选择城市
            var citySel = $("#repairCity").val();
            initSelectOptions("repairDistrict", citySel);
        }

    </script>
</body>
</html>